﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Main_Company_CreateCompany : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            // ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpire.aspx");
        }

    }
    protected void Create_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source =dis-server; initial catalog = Master; user id = sa; Password = goal;");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "create database [DISACC_" + TxtCompanyName.Text + "]";
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/XML/ConnStr.xml"));
        if (ds.Tables.Count > 0)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr["servername"] = "dis-server";
            dr["companyname"] = TxtCompanyName.Text;
            dr["dbname"] = "DISACC_" + TxtCompanyName.Text;
            dr["userid"] = "sa";
            dr["password"] = "goal";
            ds.Tables[0].Rows.Add(dr);
            ds.WriteXml(Server.MapPath("~/XML/ConnStr.xml"));
        }
        else
        {
            DataTable dt = new DataTable("connectionstring");
            dt.Columns.Add("servername", typeof(string));
            dt.Columns.Add("companyname", typeof(string));
            dt.Columns.Add("dbname", typeof(string));
            dt.Columns.Add("userid", typeof(string));
            dt.Columns.Add("password", typeof(string));
            DataRow dr = dt.NewRow();
            dr["servername"] = "dis-server";
            dr["companyname"] = TxtCompanyName.Text;
            dr["dbname"] = "DISACC_" + TxtCompanyName.Text;
            dr["userid"] = "sa";
            dr["password"] = "goal";
            dt.Rows.Add(dr);
            ds.Tables.Add(dt);
            ds.WriteXml(Server.MapPath("~/XML/ConnStr.xml"));
        }
    }
}
